/*
 * Retrieve events with a date query
 */ 

// Create the calendar service object
var calendarService = new google.gdata.calendar.CalendarService('GoogleInc-jsguide-1.0');

// The default "private/full" feed is used to retrieve events from 
// the primary private calendar with full projection 
var feedUri = 'http://www.google.com/calendar/feeds/default/private/full';

// Create a CalendarEventQuery, and specify that this query is 
// applied toward the "private/full" feed
var query = new google.gdata.calendar.CalendarEventQuery(feedUri);

// Create and set the minimum and maximum start time for the date query
var startMin = google.gdata.DateTime.fromIso8601('2008-01-01T00:00:00.000-08:00');
var startMax = google.gdata.DateTime.fromIso8601('2008-07-01T00:00:00.000-08:00');
query.setMinimumStartTime(startMin);
query.setMaximumStartTime(startMax);

// The callback that will be called when getEventsFeed() returns feed data
var callback = function(root) {

  // Obtain the array of matched CalendarEventEntry
  var eventEntries = root.feed.getEntries();

  // If there is matches for the date query
  if (eventEntries.length > 0) {
    for (var i = 0; i < eventEntries.length; i++) {
      var event = eventEntries[i];
      // Print the event title of the matches
      PRINT('Event title = ' + event.getTitle().getText());
    }
  } else {
    // No match is found for the date query
    PRINT('no events are matched from the query!');
  }
}

// Error handler to be invoked when getEventsFeed() produces an error
var handleError = function(error) {
  PRINT(error);
}

// Submit the request using the calendar service object. Notice the CalendarEventQuery 
// object is passed in place of the feed URI
calendarService.getEventsFeed(query, callback, handleError);
